const mongoose = require('../model/db');
const { userModel, Schoolmodel } = require('../model/model');

async function listRealUsers() {
    try {
        console.log('获取数据库中所有真实用户...');

        // 等待数据库连接
        await new Promise(resolve => {
            if (mongoose.connection.readyState === 1) {
                resolve();
            } else {
                mongoose.connection.once('connected', resolve);
            }
        });

        // 获取所有用户，并关联学校信息
        const users = await userModel.find({})
            .populate('schoolId', 'name abbreviation')
            .select('username studentId avatarImage isAvatarImageSet schoolId qianming');

        console.log('\n=== 数据库中的所有用户 ===');
        console.log('用户总数:', users.length);
        
        users.forEach((user, index) => {
            console.log(`${index + 1}. 用户名: ${user.username}`);
            console.log(`   学号: ${user.studentId}`);
            console.log(`   用户ID: ${user._id.toString()}`);
            console.log(`   学校: ${user.schoolId ? user.schoolId.name : '未知'}`);
            console.log(`   签名: ${user.qianming || '无'}`);
            console.log(`   头像: ${user.isAvatarImageSet ? '已设置' : '未设置'}`);
            console.log('   ---');
        });

        // 按学校分组显示
        const usersBySchool = {};
        users.forEach(user => {
            const schoolName = user.schoolId ? user.schoolId.name : '未知学校';
            if (!usersBySchool[schoolName]) {
                usersBySchool[schoolName] = [];
            }
            usersBySchool[schoolName].push(user);
        });

        console.log('\n=== 按学校分组的用户 ===');
        Object.keys(usersBySchool).forEach(schoolName => {
            console.log(`\n${schoolName} (${usersBySchool[schoolName].length}人):`);
            usersBySchool[schoolName].forEach(user => {
                console.log(`  - ${user.username} (${user.studentId})`);
            });
        });

        process.exit(0);

    } catch (error) {
        console.error('获取用户数据失败:', error);
        process.exit(1);
    }
}

// 运行脚本
listRealUsers(); 